using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace QiHe.CodeLib
{
    public class TreeViewHelper
    {
        public static List<TreeNode> Tranverse(TreeView treeView)
        {
            List<TreeNode> nodes = new List<TreeNode>();
            foreach (TreeNode node in treeView.Nodes)
            {
                nodes.AddRange(Tranverse(node));
            }
            return nodes;
        }

        public static List<TreeNode> Tranverse(TreeNode node)
        {
            List<TreeNode> nodes = new List<TreeNode>();
            nodes.Add(node);
            foreach (TreeNode subnode in node.Nodes)
            {
                nodes.AddRange(Tranverse(subnode));
            }
            return nodes;
        }

        public static List<TreeNode> GetParentNodes(TreeNode node)
        {
            List<TreeNode> nodes = new List<TreeNode>();
            TreeNode parentNode = node.Parent;
            while (parentNode != null)
            {
                nodes.Add(parentNode);
                parentNode = parentNode.Parent;
            }
            return nodes;
        }

        public static TreeNode FindTeeNode(TreeView treeView, string key)
        {
            if (key != null)
            {
                TreeNode[] nodes = treeView.Nodes.Find(key, true);
                if (nodes.Length > 0)
                {
                    return nodes[0];
                }
            }
            return null;
        }
    }
}
